package com.prawn.ift.ctrlequipment.service.impl;


import com.prawn.ift.ctrlequipment.dao.CtrlequipmentButtonDao;
import com.prawn.ift.ctrlequipment.dao.CtrlequipmentChannelDao;
import com.prawn.ift.ctrlequipment.dao.CtrlequipmentDao;
import com.prawn.ift.ctrlequipment.pojo.Ctrlequipment;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentButton;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentChannel;
import com.prawn.ift.ctrlequipment.service.CtrlEquipmentChannelService;
import entity.Result;
import entity.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.NullAttributeUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/12/15
 * @DESC： 控制设备通道服务
 */
@Service
public class CtrlEquipmentChannelServiceImpl implements CtrlEquipmentChannelService {

    @Autowired
    private CtrlequipmentChannelDao ctrlequipmentChannelDao;
    @Autowired
    private CtrlequipmentDao ctrlequipmentDao;
    @Autowired
    private CtrlequipmentButtonDao ctrlequipmentButtonDao;


    /**
     * 添加控制设备通道和控制设备
     *
     * @param ctrlEquipmentChannel
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addCtrlEquipmentChannel(CtrlequipmentChannel ctrlEquipmentChannel) {
        //根据控制通道封装控制设备信息
        Ctrlequipment ctrlEquipment = new Ctrlequipment();
        ctrlEquipment.setId(ctrlEquipmentChannel.getCtrlEquipmentChannel());
        ctrlEquipment.setNumber(ctrlEquipmentChannel.getCtrlChannelNo() + "");
        ctrlEquipment.setIsService("0");//默认关闭
        ctrlEquipment.setName(ctrlEquipmentChannel.getCtrlChannelName());
        ctrlEquipment.setTypeId(ctrlEquipmentChannel.getCtrlChannelTypeId());
        ctrlEquipment.setEquipmentId(ctrlEquipmentChannel.getEquipmentId());
        ctrlEquipment.setSceneId(ctrlEquipmentChannel.getSceneId());
        ctrlEquipment.setCreateDate(new Date());
        ctrlEquipment.setUpdateDate(new Date());

        ctrlequipmentChannelDao.save(ctrlEquipmentChannel);
        ctrlequipmentDao.save(ctrlEquipment);
        return Result.success();

    }

    /**
     * 删除控制设备通道，同时删除控制设备与对应的按钮信息
     *
     * @param id
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteCtrlEquipmentChannel(String id) {
        //删除控制设备
        CtrlequipmentChannel ctrlEquipmentChannel = ctrlequipmentChannelDao.findById(id).get();


        CtrlequipmentButton ctrlequipmentButton = new CtrlequipmentButton();
        ctrlequipmentButton.setRelId(id);
        Example example = Example.of(ctrlequipmentButton);
        List<CtrlequipmentButton> ctrlequipmentButton1 = ctrlequipmentButtonDao.findAll(example);


        //删除按钮
        if (ctrlequipmentButton1.size() != 0) {
            ctrlequipmentButtonDao.delete(ctrlequipmentButton1.get(0));
        }

        ctrlequipmentChannelDao.deleteById(id);


        Ctrlequipment ctrlequipment = new Ctrlequipment();
        ctrlequipment.setId(ctrlEquipmentChannel.getCtrlEquipmentChannel());
        Example example2 = Example.of(ctrlequipment);
        if (ctrlequipmentDao.exists(example2))
            ctrlequipmentDao.deleteById(ctrlEquipmentChannel.getCtrlEquipmentChannel());
        return Result.success();

    }

    /**
     * 修改控制设备通道
     *
     * @param ctrlEquipmentChannel
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result editCtrlEquipmentChannel(CtrlequipmentChannel ctrlEquipmentChannel) {



        Ctrlequipment ctrlEquipment = new Ctrlequipment();
        ctrlEquipment.setId(ctrlEquipmentChannel.getCtrlEquipmentChannel());
        ctrlEquipment.setNumber(ctrlEquipmentChannel.getCtrlChannelNo() + "");
        ctrlEquipment.setIsService("0");//默认关闭
        ctrlEquipment.setName(ctrlEquipmentChannel.getCtrlChannelName());
        ctrlEquipment.setTypeId(ctrlEquipmentChannel.getCtrlChannelTypeId());
        ctrlEquipment.setEquipmentId(ctrlEquipmentChannel.getEquipmentId());
        ctrlEquipment.setSceneId(ctrlEquipmentChannel.getSceneId());
        ctrlEquipment.setUpdateDate(new Date());
        ctrlequipmentDao.save(ctrlEquipment);


        CtrlequipmentChannel oldCtrlequipmentChannel =ctrlequipmentChannelDao.findById(ctrlEquipmentChannel.getId()).get();
        BeanUtils.copyProperties(ctrlEquipmentChannel,oldCtrlequipmentChannel, NullAttributeUtils.getNullPropertyNames(ctrlEquipmentChannel));

        ctrlequipmentChannelDao.save(oldCtrlequipmentChannel);
        return Result.success();

    }

    /**
     * 查询控制设备通道
     *
     * @param id
     * @return Result
     */
    @Override
    public CtrlequipmentChannel getCtrlEquipmentChannelById(String id) {
        return ctrlequipmentChannelDao.findById(id).get();
    }

    /**
     * 根据equipmentId查询CtrlequipmentChannel
     *
     * @param equipmentId
     * @return Result
     */
    @Override
    public Result getCtrlEquipmentChannelListByEquipmentId(String equipmentId) {


        List<CtrlequipmentChannel> ctrlEquipmentChannelList = ctrlequipmentChannelDao.findByEquipmentId(equipmentId);

        return Result.success().add(ctrlEquipmentChannelList);
    }

    /**
     * 查询控制设备通道列表
     *
     * @return Result
     */
    @Override
    public Result getCtrlEquipmentChannelList() {
        List<CtrlequipmentChannel> ctrlEquipmentChannelList = ctrlequipmentChannelDao.findAll();
        return Result.success().add(ctrlEquipmentChannelList);
    }




    
}
